package com.mtiiot.fstcpserver.fastdfs;

import com.mtiiot.fstcpserver.fastdfs.core.NameValuePair;
import com.mtiiot.fstcpserver.fastdfs.core.StorageClient1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by Administrator on 16-6-29.
 */
@Component("fastdfsBase")
public class FastdfsBase {
    @Autowired
    private FastdfsDataSource fastdfsDataSource;

    /**
     * 上传文件
     *
     * @param filePath
     * @return 文件存储逻辑key
     * @throws Exception
     */
    public String uploadFile(String filePath) throws Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            String uploadResult = storageClient1.upload_file1(filePath, null,
                    null);
            return uploadResult;
        }finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }

    /**
     * 上传文件 by[using byetArray]
     * @param file_buff
     * @param fileExtName 文件后缀名
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] file_buff, String fileExtName)
            throws Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            String uploadResult = storageClient1.upload_file1(file_buff,
                    fileExtName, null);
            return uploadResult;
        }finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }

    /**
     * 上传文件 by[using byetArray]
     * @param file_buff
     * @param fileExtName 文件后缀名
     * @param meta_list 元数据键值对数组
     * @return
     * @throws Exception
     */
    public String uploadFile(byte[] file_buff, String fileExtName,NameValuePair[] meta_list)
            throws Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            String uploadResult = storageClient1.upload_file1(file_buff,
                    fileExtName, meta_list);
            return uploadResult;
        }finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }

    /**
     * 下载文件
     * @param fileKey
     * @return
     * @throws java.io.IOException
     * @throws Exception
     */
    public NameValuePair[] getMetadata(String fileKey) throws IOException,
            Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            return storageClient1.get_metadata1(fileKey);
        } finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }

    /**
     * 下载文件
     * @param fileKey
     * @return
     * @throws java.io.IOException
     * @throws Exception
     */
    public byte[] downLoadFile(String fileKey) throws IOException,
            Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            byte[] r=storageClient1.download_file1(fileKey);
            return r;
        } finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }


    /**
     * 删除文件
     *
     * @param fileKey
     * @return boolean if true 成功,or失败
     * @throws Exception
     */
    public boolean deleteFile(String fileKey) throws Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            int r = storageClient1.delete_file1(fileKey);// int. when return 0 ,
            if (r == 0) {
                return true;
            } else {
                return false;
            }
        }finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }

    /**
     * 删除文件
     *
     * @param fileKey
     * @return long
     * @throws Exception
     */
    public long getCrc32(String fileKey) throws Exception {
        StorageClient1 storageClient1=fastdfsDataSource.getConnection();
        try {
            return storageClient1.get_file_info1(fileKey).getCrc32();
        }finally{
            fastdfsDataSource.releaseConnection(storageClient1);
        }
    }
}
